
/** @file integrand.h
 *
 * Interface to integrand
 *
 */

/*
 *  Copyright (C) 2007 Stefan Weinzierl
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __SECTOR_DECOMPOSITION_INTEGRAND_H__
#define __SECTOR_DECOMPOSITION_INTEGRAND_H__

#include <vector>
#include <cstddef>
#include <iostream>

#include <ginac/ginac.h>

#include "multi_index.h"

namespace sector_decomposition {

  /**
   *
   * The class integration_data contains the data which will not be modified
   * by the algorithm for the sector decomposition.
   * The data members are:
   * - n gives the number of Feynman parameters.
   * - list_feynman_parameter is a list of the Feynman parameters. It contains
   * symbols.
   * - epsilon contains the symbol of the expansion parameter.
   * - order gives the value at which we truncate the Laurent expansion.
   *
   */
  class integration_data
    {
      // ctors
    public:
      integration_data(const std::vector<GiNaC::ex> & list_feynman_parameter, GiNaC::ex epsilon, int order);

      // member functions
    public:

      // I/O operators
      friend std::ostream & operator<< (std::ostream & os, const integration_data & arg);

      // member variables 
    public:
      /// the number of Feynman parameters
      size_t n;
      /// a vector holding the Feynman parameters as GiNaC::symbol
      std::vector<GiNaC::ex> list_feynman_parameter;

      /// the symbol of the expansion parameter
      GiNaC::ex epsilon;
      /// order of the term in the Laurent series, which should be calculated
      int order;
    };

  /**
   *
   * A container class for the exponents.
   *
   * Exponents are of the form
   * \f[
   *     a + \varepsilon b,
   * \f]
   * where \f$a\f$ and \f$b\f$ are rational numbers and \f$\varepsilon\f$ is the expansion parameter.
   * The class stores \f$a\f$ and \f$b\f$ and provides methods for the addition and subtraction
   * of exponents.
   *
   */
  class exponent
    {
      // ctors
    public:
      exponent();
      exponent(int order_0, int order_1);
      exponent(GiNaC::ex order_0, GiNaC::ex order_1);

      // member functions
    public:
      GiNaC::ex operator[](size_t i) const;
      exponent & sum_up(const exponent & other);
      exponent & subtract_off(const exponent & other);
      exponent & sum_up(const exponent & other, int lambda);
      exponent & subtract_off(const exponent & other, int lambda);

      int get_integer_part() const;

      GiNaC::ex get_exponent(const integration_data & global_data) const;

      bool is_not_zero() const;

      // I/O operators
      friend std::ostream & operator<< (std::ostream & os, const exponent & arg);

      // member variables 
    public:
      /// term of order 0
      GiNaC::ex order_0;
      /// term of order 1
      GiNaC::ex order_1;
    };

  /**
   *
   * The class integrand defines an integrand of the form
   * \f[
   *     C \left( \prod\limits_{i=1}^k x_i^{\nu_0+\nu_1 \varepsilon } \right)
   *     \left[ \prod\limits_{j=1}^r \left( P_j(x) \right)^{c_0+ c_1 \varepsilon} \right].
   * \f]
   *
   * The data members are:
   *  - std::vector<size_t> index: A vector of size \f$k\f$, serving as an index for
   *      integration_data::list_feynman_parameter.
   *  - std::vector<exponent> weight: A vector holding the exponents of the single factors \f$x_i\f$.
   *  - std::vector<GiNaC::ex> polynomial_list: A vector holding the \f$r\f$ polynomials.
   *  - std::vector<exponent> poly_exponent: A vector holding the exponents of the polynomials.
   *  - GiNaC::ex extra_factor: An extra prefactor \f$C\f$.
   *
   */
  class integrand
    {
      // ctors
    public:
      integrand();
      integrand(const std::vector<size_t> & index, const std::vector<exponent> & nu,
		const std::vector<GiNaC::ex> & poly_list, const std::vector<exponent> & c);
      integrand(const std::vector<exponent> & nu,
		const std::vector<GiNaC::ex> & poly_list, const std::vector<exponent> & c);
      integrand(size_t n,
		const std::vector<GiNaC::ex> & poly_list, const std::vector<exponent> & c);
      integrand(const std::vector<size_t> & index, const std::vector<exponent> & nu,
		const std::vector<GiNaC::ex> & poly_list, const std::vector<exponent> & c,
		GiNaC::ex extra_factor);
      integrand(const std::vector<size_t> & index, const std::vector<exponent> & nu,
		GiNaC::ex extra_factor);

      // member functions
    public:
      GiNaC::ex get_integrand(const integration_data & global_data) const;
      GiNaC::ex get_weight_factor(const integration_data & global_data) const;
      GiNaC::ex get_polynomials(const integration_data & global_data) const;
      GiNaC::ex get_polynomials_without_exponent(const integration_data & global_data) const;
      GiNaC::ex get_extra_factor() const;

      bool check_homogeneous_polynomial(size_t k, const integration_data & global_data) const;
      int get_homogeneous_degree(size_t k, const integration_data & global_data) const;
      int get_degree(GiNaC::ex poly, const integration_data & global_data) const;
      void make_homogeneous(size_t k, const integration_data & global_data);
      void make_homogeneous(const integration_data & global_data);

      void restrict_to_primary_sector(size_t k, const integration_data & global_data);
      void restrict_to_primary_sector_cube(const multi_index_counter & index_set, const integration_data & global_data);

      bool check_zero(size_t k, const multi_index_ordered & index_set, const integration_data & global_data) const;
      bool check_zero(const multi_index_ordered & index_set, const integration_data & global_data) const;

      int get_ldegree(size_t k, const multi_index_ordered & index_set, const integration_data & global_data) const;
      int get_ldegree(const multi_index_ordered & index_set, const integration_data & global_data) const;

      GiNaC::ex get_polyhedra_polynomial(const integration_data & global_data) const;
      bool find_min_max(std::vector<size_t> & k_min_max_set, const integration_data & global_data);
      bool get_S_strategy_B(std::vector<size_t> & index_set, const integration_data & global_data);
      bool get_S_strategy_C(std::vector<size_t> & index_set, const integration_data & global_data);


      void create_subsector(size_t k, const std::vector<size_t> & index_set, const integration_data & global_data);
      void create_subsector(size_t k, const multi_index_ordered & index_set, const integration_data & global_data);

      int get_subtraction_vector(std::vector<size_t> & subtr_vec, std::vector<size_t> & index_set) const;

      GiNaC::ex taylor_term(GiNaC::ex polynomials, const multi_index_counter_indv & counter, const std::vector<size_t> & index_set, const integration_data & global_data) const;

      void integrated_taylor_term(const multi_index_counter_indv & counter, const std::vector<size_t> & index_set, const integration_data & global_data);

      // I/O operators
      friend std::ostream & operator<< (std::ostream & os, const integrand & arg);

      // member variables 
    public:
      /// a vector, serving as an index for integration_data::list_feynman_parameter
      std::vector<size_t> index;

      /// a vector holding the exponents of the single factors \f$x_i\f$
      std::vector<exponent> weight;

      /// a vector holding the \f$r\f$ polynomials
      std::vector<GiNaC::ex> polynomial_list;
      /// a vector holding the exponents of the polynomials
      std::vector<exponent> poly_exponent;

      /// an extra prefactor
      GiNaC::ex extra_factor;
    };

} // namespace sector_decomposition

#endif // ndef __SECTOR_DECOMPOSITION_INTEGRAND_H__

